<template>
  <el-dialog
    :title="!dataForm.orderDescId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="商品名称" prop="productName">
      <el-input v-model="dataForm.productName" placeholder="商品名称"></el-input>
    </el-form-item>
    <el-form-item label="商品图片" prop="productImg">
      <el-input v-model="dataForm.productImg" placeholder="商品图片"></el-input>
    </el-form-item>
    <el-form-item label="商品原价" prop="productPrice">
      <el-input v-model="dataForm.productPrice" placeholder="商品原价"></el-input>
    </el-form-item>
    <el-form-item label="优惠金额" prop="preferPrice">
      <el-input v-model="dataForm.preferPrice" placeholder="优惠金额"></el-input>
    </el-form-item>
    <el-form-item label="实付金额" prop="payPrice">
      <el-input v-model="dataForm.payPrice" placeholder="实付金额"></el-input>
    </el-form-item>
    <el-form-item label="购买数量" prop="payCount">
      <el-input v-model="dataForm.payCount" placeholder="购买数量"></el-input>
    </el-form-item>
    <el-form-item label="0待付款 1待收货 2待评价 3交易完成 4售后" prop="orderStatus">
      <el-input v-model="dataForm.orderStatus" placeholder="0待付款 1待收货 2待评价 3交易完成 4售后"></el-input>
    </el-form-item>
    <el-form-item label="商品外键" prop="goodId">
      <el-input v-model="dataForm.goodId" placeholder="商品外键"></el-input>
    </el-form-item>
    <el-form-item label="下单时间" prop="orderTime">
      <el-input v-model="dataForm.orderTime" placeholder="下单时间"></el-input>
    </el-form-item>
    <el-form-item label="患者外键id" prop="patientId">
      <el-input v-model="dataForm.patientId" placeholder="患者外键id"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          orderDescId: 0,
          productName: '',
          productImg: '',
          productPrice: '',
          preferPrice: '',
          payPrice: '',
          payCount: '',
          orderStatus: '',
          goodId: '',
          orderTime: '',
          patientId: ''
        },
        dataRule: {
          productName: [
            { required: true, message: '商品名称不能为空', trigger: 'blur' }
          ],
          productImg: [
            { required: true, message: '商品图片不能为空', trigger: 'blur' }
          ],
          productPrice: [
            { required: true, message: '商品原价不能为空', trigger: 'blur' }
          ],
          preferPrice: [
            { required: true, message: '优惠金额不能为空', trigger: 'blur' }
          ],
          payPrice: [
            { required: true, message: '实付金额不能为空', trigger: 'blur' }
          ],
          payCount: [
            { required: true, message: '购买数量不能为空', trigger: 'blur' }
          ],
          orderStatus: [
            { required: true, message: '0待付款 1待收货 2待评价 3交易完成 4售后不能为空', trigger: 'blur' }
          ],
          goodId: [
            { required: true, message: '商品外键不能为空', trigger: 'blur' }
          ],
          orderTime: [
            { required: true, message: '下单时间不能为空', trigger: 'blur' }
          ],
          patientId: [
            { required: true, message: '患者外键id不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.orderDescId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.orderDescId) {
            this.$http({
              url: this.$http.adornUrl(`/order/orderdesc/info/${this.dataForm.orderDescId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.productName = data.orderDesc.productName
                this.dataForm.productImg = data.orderDesc.productImg
                this.dataForm.productPrice = data.orderDesc.productPrice
                this.dataForm.preferPrice = data.orderDesc.preferPrice
                this.dataForm.payPrice = data.orderDesc.payPrice
                this.dataForm.payCount = data.orderDesc.payCount
                this.dataForm.orderStatus = data.orderDesc.orderStatus
                this.dataForm.goodId = data.orderDesc.goodId
                this.dataForm.orderTime = data.orderDesc.orderTime
                this.dataForm.patientId = data.orderDesc.patientId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/order/orderdesc/${!this.dataForm.orderDescId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'orderDescId': this.dataForm.orderDescId || undefined,
                'productName': this.dataForm.productName,
                'productImg': this.dataForm.productImg,
                'productPrice': this.dataForm.productPrice,
                'preferPrice': this.dataForm.preferPrice,
                'payPrice': this.dataForm.payPrice,
                'payCount': this.dataForm.payCount,
                'orderStatus': this.dataForm.orderStatus,
                'goodId': this.dataForm.goodId,
                'orderTime': this.dataForm.orderTime,
                'patientId': this.dataForm.patientId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
